package secretary;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKCashAdvance;
import controllers.Logs;
import controllers.Registered;
import controllers.StudentInformation;

public class EditProfile extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model studentIdModel;
	private Model yearLevelModel;
	private Model firstNameModel;
	private Model middleNameModel;
	private Model lastNameModel;
	private Model genderModel;
	private Model birthdayModel;
	private Model addressModel;
	private Model mobileModel;
	private Model passwordModel;
	private Model fieldModel;
	private DropDownChoice yearlevel, sex;
	private List number = Arrays.asList(new String[] { "1", "2", "3", "4" });
	private List choice = Arrays.asList(new String[] { "Male", "Female" });
	
	
	public EditProfile(String id) throws IOException, SQLException
	{
		add(new BookmarkablePageLink("home", SecretaryHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new FeedbackPanel("error"));
		
		final String studentid= id;
		StudentInformation info = (StudentInformation)sqlMap.queryForObject("StudentInformation.MyInformation", studentid);
	
		studentIdModel = new Model(studentid);
		firstNameModel = new Model(info.getStudent_firstName());
		middleNameModel = new Model(info.getStudent_middleName());
		lastNameModel = new Model(info.getStudent_lastName());
		yearLevelModel = new Model(info.getStudent_yearlvl());
		genderModel = new Model(info.getStudent_gender());
		birthdayModel = new Model(info.getStudent_birthdate());
		addressModel = new Model(info.getStudent_address());
		mobileModel = new Model(info.getStudent_contNo());
		fieldModel = new Model (info.getStudent_hobby());
	
		Form form = new Form("editForm");
		
		final String uploadby = ((Sessions)getSession()).getDesc();
		
		Date today = new Date();
		Timestamp timestamp = new Timestamp(today.getTime());
		final String ts = timestamp.toString();
		
		form.add(new Button("cancel"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  ManageStudentAccounts page = new ManageStudentAccounts();
			setResponsePage(page);
			}
		});
		
		form.add(new Button("save"){
			private static final long serialVersionUID = 1L;

			public void onSubmit(){
			  try{
				  	String studentid = (String)studentIdModel.getObject();
					String yearLevel = (String) yearLevelModel.getObject();
					String firstName = (String) firstNameModel.getObject();
					String middleName = (String) middleNameModel.getObject();
					String lastName = (String) lastNameModel.getObject();
					String gender = (String) genderModel.getObject();
					String birthday = (String) birthdayModel.getObject();
					String address = (String) addressModel.getObject();
					String mobile = (String) mobileModel.getObject();  
					String field = (String) fieldModel.getObject(); 
				  
				  
					String uploader = ((Sessions)getSession()).getDesc();
					
					Date today = new Date();
					Timestamp timestamp = new Timestamp(today.getTime());
					String ts = timestamp.toString();
				  
					StudentInformation info = new StudentInformation();
					info.setStudent_id(studentid);
					info.setStudent_yearlvl(yearLevel);
					info.setStudent_firstName(firstName);
					info.setStudent_middleName(middleName);
					info.setStudent_lastName(lastName);
					info.setStudent_birthdate(birthday);
					info.setStudent_gender(gender);
					info.setStudent_address(address);
					info.setStudent_contNo(mobile);
					info.setStudent_hobby(field);
					sqlMap.update("StudentInformation.updateInformation", info);
					
					Registered reg = new Registered();
					reg.setStudent_id(studentid);
					reg.setStudent_name(firstName + " " + middleName + " " + lastName);
					reg.setStudent_yearlevel(yearLevel);
					sqlMap.update("Registered.updateRegistration", reg);
		
					ViewProfile page = new ViewProfile(studentid);
					setResponsePage(page);
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		TextField studentId = new TextField("studentId", studentIdModel);
		form.add(studentId);
		
		TextField firstName = new TextField("firstName", firstNameModel);
		form.add(firstName);
		
		sex = new DropDownChoice("gender", genderModel, choice);
		form.add(sex);
		
		yearlevel =new DropDownChoice("yearLevel", yearLevelModel, number);
		form.add(yearlevel);
		
		TextField middleName = new TextField("middleName", middleNameModel);
		form.add(middleName);
		
		TextField address = new TextField("address",addressModel);
		form.add(address);
		
		TextField lastName = new TextField("lastName", lastNameModel);
		form.add(lastName);
		
		TextField birthday = new TextField("birthday", birthdayModel);
		form.add(birthday);
		
		TextField contact = new TextField("contact", mobileModel);
		form.add(contact);

		TextField hobby = new TextField("hobby", fieldModel);
		form.add(hobby);
		
		
		
		add(form);
	}

}
